<?php
namespace app\member\controller;
use app\AdminRest;

use app\member\model\CashRecord;
use app\member\model\Member;
use app\member\model\StoredOrder;
use longbingcore\wxcore\WxPay;
use think\App;
use app\member\model\Stored as model;
use think\facade\Db;


class AdminStored extends AdminRest
{

    protected $model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-09 10:35
     * @功能说明:列表
     */
    public function storedList(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $data = $this->model->storedList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-09 10:41
     * @功能说明:储值添加
     */
    public function storedAdd(){

        $input = $this->_input;

        $input['uniacid'] = $this->_uniacid;

        $res = $this->model->storedAdd($input);

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2020-07-15 11:21
     * @功能说明:会员配置详情
     */
    public function storedInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->storedInfo($dis);

        if(empty($data)){

            $this->errorMsg('数据未找到');
        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-07-15 13:24
     * @功能说明:配置编辑
     */
    public function storedUpdate(){

        $input = $this->_input;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->storedUpdate($dis,$input);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-09 13:39
     * @功能说明:下拉框
     */
    public function storedSelect(){

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['status','>',-1];

        $data = $this->model->storedSelect($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-16 11:09
     * @功能说明:员工扣款
     */
    public function staffDeduction(){

        $input = $this->_input;

        $member_model = new Member();
        //查看会员信息
        $member_info = $member_model->memberUpdateInfo(['user_id'=>$input['id']]);

        $type = !empty($input['type'])?$input['type']:1;
        //判断余额
        if($input['price']>$member_info['stored']&&$type!=4){

            $this->errorMsg('储值不足');
        }

        $order_model = new StoredOrder();

        $res = $order_model->desStore($input['price'],$input['content'],$member_info,$type,$this->_user['admin_id']);

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-01 11:23
     * @功能说明:用户提现记录
     */
    public function userCashList()
    {
        $input = $this->_input;

        $cash_model = new CashRecord();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['name'])){

            $dis[] = ['b.nickName','like','%'.$input['name'].'%'];
        }


        $data = $cash_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 19:08
     * @功能说明:同意申请打款
     */
    public function cashPass(){

        $input = $this->_input;

        $cash_model = new CashRecord();

        $record = $cash_model->dataInfo(['id'=>$input['id']]);

        if($record['status']!=1){

            $this->errorMsg('状态错误');
        }

        $update = [

            'status' => 2,

            'hx_time'=> time(),

            'hx_user'=> $this->_user['admin_id'],

            'type'   => $input['type']
        ];
        Db::startTrans();

        $res = $cash_model->where(['id'=>$input['id']])->update($update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }

        if($input['type']==1){

            $openid = Db::name('longbing_card_user')->where(['id'=>$record['user_id']])->value('openid');
            //微信相关模型
            $wx_pay = new WxPay($this->_uniacid);
            //微信提现
            $res    = $wx_pay->crteateMchPay($openid,$record['true_price']);

            if($res['result_code']=='SUCCESS'&&$res['return_code']=='SUCCESS'){
                //if(1==1){

            }else{

                Db::rollback();

                return $this->error(!empty($res['err_code_des'])?$res['err_code_des']:'你还未该权限');

            }
        }

        Db::commit();

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 19:08
     * @功能说明:不同意申请打款
     */
    public function cashNoPass(){

        $input = $this->_input;

        $cash_model = new CashRecord();

        $record = $cash_model->dataInfo(['id'=>$input['id']]);

        if($record['status']!=1){

            $this->errorMsg('状态错误');
        }

        $update = [

            'status' => -1,

            'hx_time'=> time(),

            'hx_user'=> $this->_user['admin_id']

        ];

        Db::startTrans();

        $res = $cash_model->where(['id'=>$input['id']])->update($update);

        if($res!=1){

            Db::rollback();

            $this->errorMsg('审核失败');

        }

        $member_model= new Member();

        $order_model = new StoredOrder();

        $member_info = $member_model->memberUpdateInfo(['user_id'=>$record['user_id']]);

        $record_id = $input['id'];
        //添加提现记录并且退还余额
        $res = $order_model->desStore($record['true_price'],'',$member_info,8,$this->_user['admin_id'],'',$record_id);

        if($res!=1){

            $this->errorMsg('申请失败');
        }
        Db::commit();

        return $this->success($res);

    }










}
